package com.seeyon.apps.kk.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.kk.manager.AdapterSyncDataManager;

public class TTUtils {
	private static final Log log = LogFactory.getLog(TTUtils.class);
	@SuppressWarnings("deprecation")
	public static String ttPost(String url,String jsonData){
		String result="";
	 try{					
		 PostMethod post = new PostMethod(url);
         HttpClient httpClient=new HttpClient();		  		
		 post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
		 post.setRequestBody(jsonData);
		 post.addRequestHeader("accept","application/json");
		 post.addRequestHeader("content-type", "application/json");			
		 httpClient.executeMethod(post);			  
		 result =new String(post.getResponseBody(),"utf-8");
		}catch(Exception e){
			return e.toString();
		}
      return result;
	} 
	
	
	public static String ttGet(String url){
		String result="";
	 try{					
		 GetMethod get = new GetMethod(url);
         HttpClient httpClient=new HttpClient();		  		
         get.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
         get.addRequestHeader("accept","application/json");		
		 httpClient.executeMethod(get);			  
		 result =new String(get.getResponseBody(),"utf-8");
		}catch(Exception e){
			return e.toString();
		}
      return result;
	} 
	/**
	 * 获取post请求中的参数
	 * 
	 * @param args
	 */
	public static JSONObject ttGetRequestBody(HttpServletRequest request) {
		JSONObject resultjson=new JSONObject();
		try {
			String str = "";
			String wholeStr = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					request.getInputStream(), "UTF-8"));
			while ((str = reader.readLine()) != null) {// 一行一行的读取body体里面的内容；
				wholeStr += str;
			}
			 resultjson = JSONObject.parseObject(wholeStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultjson;
	}
	/**
	 * 调用sims接口
	 * @param url  接口地址
	 * @param obj  参数
	 * @return
	 */
	public static JSONArray  simsResource(String url,String token,String interfaceNo){
	    JSONArray resultAry=new JSONArray(); 
		boolean over = false;//是否结束本次循环
	     int PageNo=1;
	     while (!over) {
			 JSONObject obj=new JSONObject();
			 JSONArray paramAry=new JSONArray();
			 JSONObject paramObj=new JSONObject();
			 paramObj.put("token", token);
			 paramObj.put("interfaceNo", interfaceNo);
			 paramObj.put("PageNo",String.valueOf((PageNo++)));
			 paramObj.put("PageSize", "5000");
			 paramAry.add(paramObj);
			 obj.put("param", paramAry);		 
			 JSONObject result=JSONObject.parseObject(ttPost(url, obj.toJSONString()));		     
		     if("true".equals(result.getString("success"))){
		    	 if(result.getInteger("totalCount")==0){
		    		 over=true;
		    	 }
		    	 JSONArray  ary=result.getJSONArray("data");
		    	 resultAry.addAll(ary);
		     }else{
		    	 log.info("ddkk调用sims接口数据出错："+result.getString("msg"));
		    	 over=true;
		     }
		}
		return resultAry;
	}
	
	public static void main(String[] args) {
		JSONArray result=simsResource("https://interdb.jjmc.cn/WebAPI/Service/WebAPI.ashx", "70259133-9992-4C00-BA2F-8987CCCA45DC", "IN036");
		System.out.println(result.size());
	}
}
